// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OriginValueConverter.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Navigation.Visualization
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    using System.Windows.Media;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Class which converts the origin pose to path geometry.
    /// </summary>
    [ValueConversion(typeof(Pose2D), typeof(PathGeometry))]
    public class OriginValueConverter : IValueConverter
    {
        /// <summary>
        /// Converts the particle poses to path geometry.
        /// </summary>
        /// <param name="value">Particle poses array.</param>
        /// <param name="targetType">PathGeometry to be created.</param>
        /// <param name="parameter">The parameter is not used.</param>
        /// <param name="culture">The parameter is not used.</param>
        /// <returns>Path geometry</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Pose2D origin = (Pose2D)value;
            PathFigureCollection pathFigureCollection = new PathFigureCollection();

            int originX = (int)origin.X;
            int originY = (int)origin.Y;
            int originLength = 10;

            System.Windows.Point[] startPoints = 
            {
                new System.Windows.Point(originY - originLength, originX),
                new System.Windows.Point(originY, originX - originLength) 
            };
            System.Windows.Point[] endPoints = 
            { 
                new System.Windows.Point(originY + originLength, originX),
                new System.Windows.Point(originY, originX + originLength) 
            };
            for (int i = 0; i < startPoints.Length; ++i)
            {
                PathFigure pathFigure = new PathFigure();
                pathFigure.StartPoint = startPoints[i];

                LineSegment lineSegment = new LineSegment();
                lineSegment.Point = endPoints[i];

                PathSegmentCollection pathSegmentCollection = new PathSegmentCollection();
                pathSegmentCollection.Add(lineSegment);
                pathFigure.Segments = pathSegmentCollection;
                pathFigureCollection.Add(pathFigure);
            }

            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures = pathFigureCollection;
            pathGeometry.Freeze();

            return pathGeometry;
        }

        /// <summary>
        /// This method is not implemented.
        /// </summary>
        /// <param name="value">The parameter is not used.</param>
        /// <param name="targetType">The parameter is not used.</param>
        /// <param name="parameter">The parameter is not used.</param>
        /// <param name="culture">The parameter is not used.</param>
        /// <returns>Throws not implemented exception</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
